<?php
/**
 * This is the model class for table "company_members".
 */
class company_members extends ActiveRecord
{
	/**
	 * The followings are the available columns in table 'company_members':
	 * @var integer $id
	 * @var string $name
	 * @var string $type
	 * @var integer $company_id
	 */

	/**
	 * Returns the static model of the specified AR class.
	 * @return company_members the static model class
	 */
	public $getContact,$getEmail;
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'company_members';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('type, company_id', 'required'),
			array('company_id', 'numerical', 'integerOnly'=>true),
			array('company_id','exist','className'=>'company','attributeName'=>'id'),
			array('name', 'length', 'max'=>50),
			array('type', 'length', 'max'=>25),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, type, company_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'contacts'=>array(self::HAS_MANY,'company_members_contacts','foreign_key','condition'=>'for_model="member"'),
			'email'=>array(self::HAS_MANY,'company_members_email','foreign_key','condition'=>'for_model="member"'),
		);
	}
	public function afterFind(){
		$this->getContact = $this->getContact();
		$this->getEmail = $this->getEmail();
		return parent::afterFind();
	}
	public function getContact(){
		if($this->isNewRecord)
			return '';
		$_contact = array();
		foreach($this->contacts as $contact) {
			$_contact[] = '(' . $contact->type . ')'.$contact->ref;
		}
		return implode(',',$_contact);
	}
	public function getEmail(){
		if($this->isNewRecord)
			return '';
		$_email = array();
		foreach($this->email as $email)
			$_email[] = '('.$email->type.')'.$email->ref;
		return implode(', ',$_email);
	}
	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Name',
			'type' => 'Type',
			'company_id' => 'Company',
		);
	}
	public function AllMember(){
		$criteria = new CDbCriteria;
		$criteria->compare('company_id',$this->company_id);
		return new CActiveDataProvider(get_class($this),array(
			'criteria'=>$criteria,
			'pagination'=>array(
       			'pageSize'=>5,
    		),
		));
	}
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);

		$criteria->compare('name',$this->name,true);

		$criteria->compare('type',$this->type,true);

		$criteria->compare('company_id',$this->company_id);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
}